﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace homework5
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool dotted = false;
        private Cal caculator = new Cal();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Input(object sender, RoutedEventArgs e)
        {
            display.Text += ((Button)sender).Content;
            if (display.Text.Length > 11)
            {
                display.Text = "ERROR";
                DisableInput();
            }
        }

        private void Backspace(object sender, RoutedEventArgs e)
        {
            if (display.Text.Length > 0)
            {
                if (display.Text.ElementAt(display.Text.Length - 1) == '.')
                    dotted = false;
                display.Text = display.Text.Substring(0, display.Text.Length - 1);
            }
        }

        private void Operator(object sender, RoutedEventArgs e)
        {
            if (display.Text.Length > 0 && display.Text.ElementAt(display.Text.Length - 1) >= 48 && display.Text.ElementAt(display.Text.Length - 1) <= 57)
            {
                display.Text += ((Button)sender).Content;
                dotted = false;
            }
            if(display.Text.Length ==0 &&((Button)sender).Content.ToString()=="-")
                display.Text+= "-";
            if (display.Text.Length > 11)
            {
                display.Text = "ERROR";
                DisableInput();
            }
        }

        private void Dot(object sender, RoutedEventArgs e)
        {
            if (display.Text.Length > 0&&!dotted&& display.Text.ElementAt(display.Text.Length - 1) >= 48 && display.Text.ElementAt(display.Text.Length - 1) <= 57)
            {
                display.Text += ((Button)sender).Content;
                dotted = true; 
            }
            if (display.Text.Length > 11)
            {
                display.Text = "ERROR";
                DisableInput();
            }

        }

        private void Equal(object sender, RoutedEventArgs e)
        {
            if (display.Text.Length > 0)
            {
                if (display.Text.ElementAt(display.Text.Length - 1) >= 48 && display.Text.ElementAt(display.Text.Length - 1) <= 57)
                {
                    display.Text = caculator.getResult(display.Text);
                    if (display.Text.Contains('.'))
                        dotted = true;
                    if (display.Text.Length > 11)
                        display.Text = "ERROR";
                    if (display.Text == "ERROR")
                        DisableInput();
                }
            }
        }
        private void Clear(object sender, RoutedEventArgs e)
        {
            display.Text = "";
            dotted = false;
            EnableInput();
        }

        private void DisableInput() {
            button0.IsEnabled = false;
            button1.IsEnabled = false;
            button2.IsEnabled = false;
            button3.IsEnabled = false;
            button4.IsEnabled = false;
            button5.IsEnabled = false;
            button6.IsEnabled = false;
            button7.IsEnabled = false;
            button8.IsEnabled = false;
            button9.IsEnabled = false;
            ADD.IsEnabled = false;
            SUB.IsEnabled = false;
            MULTIPLY.IsEnabled = false;
            DIVIDE.IsEnabled = false;
            back.IsEnabled = false;
            equal.IsEnabled = false;
            dot.IsEnabled = false;
        }

        private void EnableInput()
        {
            button0.IsEnabled = true;
            button1.IsEnabled = true;
            button2.IsEnabled = true;
            button3.IsEnabled = true;
            button4.IsEnabled = true;
            button5.IsEnabled = true;
            button6.IsEnabled = true;
            button7.IsEnabled = true;
            button8.IsEnabled = true;
            button9.IsEnabled = true;
            ADD.IsEnabled = true;
            SUB.IsEnabled = true;
            MULTIPLY.IsEnabled = true;
            DIVIDE.IsEnabled = true;
            back.IsEnabled = true;
            equal.IsEnabled = true;
            dot.IsEnabled = true;
        }
    }
}